package com.example.springbootmqdemo.aajuc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Classname: Thread1
 *
 * @Author: Jam
 * @Create 2022/12/2 - 18:34
 * @Version 1.0   Lock版的生产者 消费者模式 精准通知
 */
public class Thread5 {
    public static void main(String[] args) {
        Ticketk5 ticket3  = new Ticketk5();

        //lambda表达式 (参数)->{代码}
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    ticket3.printA();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    ticket3.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    ticket3.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();
    }
}

//判断等待、业务、通知
class Ticketk5{ //资源类
    //属性 方法
    private  Lock lock = new ReentrantLock();
    private Condition  Condition1 = lock.newCondition(); //监听printA
    private Condition  Condition2 = lock.newCondition(); //监听printB
    private Condition  Condition3 = lock.newCondition(); //监听printC
    private int number = 1 ; //1A 2B 3C

    public  void printA() throws InterruptedException {

        lock.lock();//1.加锁

        try {
            //2.业务代码start  业务判断->执行->通知
                while(number != 1){
                    //等待
                    Condition1.await();
                }
            System.out.println(Thread.currentThread().getName()+"--AAAA-");
            number = 2;
            //唤醒 指定的人：B
            Condition2.signal();
            //业务代码end
        } catch (Exception e) {
            e.getStackTrace();
        }finally {
            lock.unlock();//3.解锁
        }
    }

    public  void printB() throws InterruptedException {

        lock.lock();//1.加锁

        try {
            //2.业务代码start  业务判断->执行->通知
            while(number != 2){
                //等待
                Condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"--BBBB-");
            number = 3;
            //唤醒 指定的人：B
            Condition3.signal();
            //业务代码end
        } catch (Exception e) {
            e.getStackTrace();
        }finally {
            lock.unlock();//3.解锁
        }
    }

    public  void printC() throws InterruptedException {

        lock.lock();//1.加锁

        try {
            //2.业务代码start  业务判断->执行->通知
            while(number != 3){
                //等待
                Condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"--CCCC-");
            number = 1;
            //唤醒 指定的人：A
            Condition1.signal();
            //业务代码end
        } catch (Exception e) {
            e.getStackTrace();
        }finally {
            lock.unlock();//3.解锁
        }
    }
}